<template>
	<view class="classSchedule">
		<view class="table">
			<view class="thead">
				<view class="th">
					课时/日期
				</view>
				<view class="th" v-for="(item,i) in table.weeks" :key="i">
					{{item}}
				</view>
			</view>
			<view class="tbody">
				<view class="tr" v-for="(step,index) in table.period" :key="index">
					<view class="td">
						{{step}}
					</view>
					<view class="td" v-for="(item,i) in table.weeks" :key="i" @click="selectDay(index,i)" :class="{active:selects[`${i}_${index}`]}">
						{{selects[`${i}_${index}`]?'有课':'空闲'}}
					</view>
				</view>
			</view>
		</view>
		<!-- 按钮区域 -->
		<view class="btns flexR">
			<view class="btn center hover" @click="resetSelect">
				重置
			</view>
			<view class="btn center hover" @click="saveSelect">
				保存
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				table: {
					weeks: ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日', ],
					period: ['第一节', '第二节', '第三节', '第四节', '第五节', '第六节', '第七节', '第八节', ]
				},
				selects: {}
			}
		},
		methods: {
			selectDay(period, week) {
				console.log(week, period)
				let key = `${week}_${period}`
				this.$set(this.selects, key, !this.selects[key])
			},
			async resetSelect() {
				let flage = await this.$showModal({
					title: '啊啊',
					content: '您确定要重置表单吗？'
				})
				flage && this.$set(this, 'selects', {})
			},
			saveSelect() {
				let selects = Object.keys(this.selects).filter(key => this.selects[key]).sort((a, b) => {
					let [week, period] = a.split('_')
					let [week1, period1] = b.split('_')
					if (week > week1) {
						return 1
					} else if (week == week1) {
						if (period > period1) return 1
						return -1
					} else {
						return -1
					}
				}).map(item => {
					let [week, period] = item.split('_')
					return `${this.table.weeks[week]}-${this.table.period[period]}`
				})
				console.log(selects)
				alert(selects.join(';'))
			}
		}
	}
</script>

<style lang="scss">
	.classSchedule {
		padding-top: 35rpx;
	}

	.table {

		.th,
		.td {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: center;
			height: 65rpx;
			margin: 25rpx 4rpx;
			border-radius: 10rpx;
		}

		.thead {
			display: flex;

			.th {}
		}

		.tbody {
			.tr {
				display: flex;

				.td {
					&:not(:first-child) {
						background-color: $mainColorP;
						color: white;

						&:active {
							transform: translateY(4rpx);
							box-shadow: 0 -5rpx 8rpx 0 rgba(39, 40, 34, 0.3);
						}
					}

					&.active {
						background-color: $mainColor;
					}
				}
			}
		}
	}

	.btns {
		padding-top: 25rpx;

		.btn {
			width: 160rpx;
			height: 65rpx;
			background-color: $mainColor;
			color: white;
			border-radius: 8rpx;

			&+.btn {
				margin-left: 15rpx;
			}
		}
	}
</style>
